<?php

Class Moduser extends Model {
    
    private $error = array();
    
    function Moduser() {
        
        parent::Model();
    }
    
    public function get_errors($clear = true) {
        
        $result = '';
        
        if (count($this->error) <= 0) {
            
            return false;
        }
        
        foreach($this->error as $row) {
            
            $result .= $row.'<br>';
        }
        
        if ($clear) {
            
            $this->error = array();
        }
        
        return $result;
    }
    
    public function add($login, $mail, $password, $group = false) {
        
        $query = $this->db->get_where('users', array('user_login' => $login));
        
        if( $query->num_rows() == 1 ) {
            $this->error[] = $this->lang->line('error_register_login');
            return false;
        }
        
        $query = $this->db->get_where('users', array('user_mail' => $mail));
        
        if( $query->num_rows() == 1 ) {
            $this->error[] = $this->lang->line('error_register_mail');
            return false;
        }
        
        $data = array(
            'user_login'    => $login,
            'user_password' => $this->usr->encode_password($password),
            'user_mail'     => $mail,
            'user_datereg ' => time(),
            'user_group'    => ($group === false) ? $this->usr->for_register : $group
            );
                        
        $this->db->insert('users',$data);
        return true;
    }
    
    public function edit($id, $login, $mail, $password, $group = false) {
        
        $query = $this->db->get_where('users', array('user_login' => $login));
        $usr = $query->row_array();
        
        if( $query->num_rows() == 1 && $id != $usr['user_id']) {
            $this->error[] = $this->lang->line('error_register_login');
            return false;
        }
        
        $query = $this->db->get_where('users', array('user_mail' => $mail));
        $usr = $query->row_array();
        
        if( $query->num_rows() == 1 && $id != $usr['user_id']) {
            $this->error[] = $this->lang->line('error_register_mail');
            return false;
        }
        
        $data = array(
            'user_login'    => $login,
            'user_mail'     => $mail,
            'user_group'    => ($group === false) ? $this->defult_group : $group
        );
            
        if ($password != '') {
            
            $data['user_password'] = $this->usr->encode_password($password);
        }    
                        
        $this->db->update('users', $data, array('user_id' => $id));
        return true;
    }
    
    public function login($login, $password) {
        
        $query = $this->db->get_where('users', array('user_login' => $login));
        
        if($query->num_rows() == 0) {
           $this->error[] = $this->lang->line('error_login');
           return false;
        }
        
        $user = $query->row_array();
        
        if ($this->usr->encode_password($password) != $user['user_password']) {
            $this->error[] = $this->lang->line('error_login');
            return false;           
        }
        
        $this->session->set_userdata(array('login' => $login, 'password' => $password));
        return true;
    }
    
    public function logout() {
        
        $this->session->sess_destroy();
        $this->usr->group = $this->usr->for_guest;
        return true;
    }
    
    public function get_users($were = array(), $limit = NULL, $offset = NULL) {
        
        if (count($were) > 0) {
            
            return $this->db->get_where('users', $were);
        }
        
        return $this->db->get('users');
    }
    
    public function count_users() {
        
        return $this->db->count_all('users');
    }
    
    public function get_groups($id = false) {
        
        if ($id) {
            
            $this->db->where('id', $id);
        }
        
        return $this->db->get('groups');
    }
    
    public function delete_user($id) {
        
        $this->db->delete('users', array('user_id' => $id)); 
    }
    
    public function add_group($title, $copy) {
        
        $this->db->insert('groups',array('title' => $title));
        $group_id = $this->db->insert_id();
        
        $rights = $this->db->get_where('rights', array('role' => $copy));
        
        if ($rights->num_rows() <= 0) {
            
            return true;
        }
        
        $this->db->trans_start();
        foreach($rights->result_array() as $row) {
            
            unset($row['id']);
            $row['role'] = $group_id;
            
            $this->db->insert('rights', $row);
        }
        $this->db->trans_complete();
        
        return true;
    }
    
    public function delete_group($id, $move) {
        
        $this->db->delete('rights', array('role' => $id));
        $this->db->delete('groups', array('id' => $id));
        
        $users = $this->get_users(array('user_group' => $id));
        
        if ($users->num_rows() <= 0) {
            
            return true;
        }
        
        foreach($rights->result_array() as $row) {
            
            $this->db->update('users', array('user_group' => $move), array('user_id' => $row['user_id']));
        }
        
        return true;
    }
    
    public function edit_group($id, $title, $is_guest = false, $is_register = false) {
        
        if ($is_guest == 1) {
            
            $this->db->update('groups', array('is_guest' => 0));
            $update['is_guest'] = 1;
        }
        
        if ($is_register == 1) {
            
            $this->db->update('groups', array('is_register' => 0));
            $update['is_register'] = 1;
        }
        
        $update['title'] = $title;
        
        $this->db->update('groups', $update, array('id' => $id));
    }
}